#include <string.h>
#include "str/PBufStr.h"
#include "BSocketFile.h"

void PBufStr_init(PBufStr *str, char *buf, unsigned int buf_len) {
    str->buf = buf;
    str->buf_len = buf_len;
    str->str_len = 0;
    str->resolver_start = buf;
    str->resolver_end = buf;
}

int PBufStr_reset(PBufStr *str) {
    str->str_len = 0;
    str->buf[str->str_len] = '\0';
    str->resolver_len = 0;
    str->ridx = 0;
    str->resolver_start = str->buf;
    str->resolver_end = str->buf;
    return PBUFSTR_RET_SUCCEED;
}

int PBufStr_char_add(PBufStr *str, char ch) {
    if (str->str_len + 1 >= str->buf_len) {
        return PBUFSTR_RET_ERROR;
    }
    str->buf[str->str_len] = ch;
    str->str_len++;
    str->buf[str->str_len] = '\0';
    return PBUFSTR_RET_SUCCEED;
}

int PBufStr_str_add(PBufStr *str, const char *add_str) {
    char *addstrptr = (char *) add_str;
    while (*addstrptr != 0) {
        if (str->str_len + 1 >= str->buf_len) {
            return PBUFSTR_RET_ERROR;
        }
        str->buf[str->str_len] = *addstrptr;
        addstrptr++;
        str->str_len++;
    }
    str->buf[str->str_len] = '\0';
    return PBUFSTR_RET_SUCCEED;
}

int PBufStr_str_addn(PBufStr *str, const char *add_str, unsigned int add_len) {
    if (str->str_len + add_len >= str->buf_len) {
        return PBUFSTR_RET_ERROR;
    }
    for (unsigned int i = 0; i < add_len; i++) {
        str->buf[str->str_len] = add_str[i];
        str->str_len++;
    }
    str->buf[str->str_len] = '\0';
    return PBUFSTR_RET_SUCCEED;
}

int PBufStr_find_endchar(PBufStr *str, char endch) {
    str->resolver_len = 0;
    str->resolver_start = str->resolver_end;
    for (unsigned int i = str->ridx; i < str->str_len; i++) {
        if (str->buf[str->ridx] == endch) {
            str->ridx++;
            str->resolver_end += str->resolver_len +1 ;
            return PBUFSTR_RET_SUCCEED;
        }
        str->resolver_len++;
        str->ridx++;
//        str->resolver_end++;
    }
    return PBUFSTR_RET_ERROR;
}

int PBufStr_find_endstr(PBufStr *str, char *endch) {
    char *p;
    str->resolver_start = str->resolver_end;
    p = strstr(str->buf + str->ridx, endch);
    if (p != 0) {
        str->resolver_len = p - str->buf + str->ridx;
        str->ridx += str->resolver_len + strlen(endch);
        str->resolver_end += str->resolver_len + strlen(endch);
    }
    return PBUFSTR_RET_ERROR;
}

void PBufStr_skip_char(PBufStr *str, char ch) {
    str->resolver_len = 0;
    str->resolver_start = str->resolver_end;
    if ((str->buf[str->ridx] == ch) && (str->ridx <= str->str_len)) {
        str->ridx++;
        str->resolver_len++;
        str->resolver_end++;
    }

}

void PBufStr_skip_nchar(PBufStr *str, char ch) {
    str->resolver_len = 0;
    str->resolver_start = str->resolver_end;
    while (str->ridx <= str->str_len) {
        if (str->buf[str->ridx] == ch) {
            str->ridx++;
            str->resolver_len++;
            str->resolver_end++;
        } else {
            break;
        }
    }
}

int PBufStr_cut_endchar(PBufStr *str, char endch) {
    str->resolver_len = 0;
    str->resolver_start = str->resolver_end;
    for (unsigned int i = str->ridx; i < str->str_len; i++) {
        if (str->buf[str->ridx] == endch) {
            str->buf[str->ridx] = '\0';
            str->ridx++;
            str->resolver_end += str->resolver_len +1 ;
            return PBUFSTR_RET_SUCCEED;
        }
        str->resolver_len++;
        str->ridx++;
    }
    return PBUFSTR_RET_ERROR;
}



int PBufStr_str_save(PBufStr *str, const char *add_str) {
    PBufStr_reset(str);
    char *addstrptr = (char *) add_str;
    while (*addstrptr != 0) {
        if (str->str_len + 1 >= str->buf_len) {
            return PBUFSTR_RET_ERROR;
        }
        str->buf[str->str_len] = *addstrptr;
        addstrptr++;
        str->str_len++;
    }
    str->buf[str->str_len] = '\0';
    return PBUFSTR_RET_SUCCEED;
}

